/**
 * 
 */
package org.lookouster.mopg.connector;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.lookouster.mopg.bo.Bill;

/**
 * @author lkster
 * Bills integration
 */
public class BillFileImporter {
	
	
	
	public final String BFI_STR1 = "File cannot be found";
	public final String BFI_STR2 = "File cannot be read";
	public final String BFI_STR3 = "Empty file";
	public final String BFI_STR4 = " duplicate records";
	public final String BFI_STR5 = " bills without number";
	public final String BFI_STR6 = "Import failed";
	public final String BFI_STR7 = " bill numbers already exist in the database";
	public final String BFI_STR8 = "Import successful";
	
	String billFilePath;
	String delimiter;
	int provider;
	int rowCount, validBillNoCount, emptyBillNoCount, duplicatesCount, existingBillNoCount;
	Double totalAmount, totalValidBillNoAmount, totalEmptyBillNoAmount;
		
	/**
	 * 1st check: File existence
	 * 2nd check: File read
	 * 3rd check: Total Amount
	 * 4th check: Bill Number
	 * @param provider
	 * @param path
	 * @param delim
	 */
	public BillFileImporter(int provider, String path, String delim)
	{
		billFilePath = path;
		delimiter = delim;
		this.provider = provider;
	}
	
	public boolean runCheck()
	{
		//Read the whole file into a buffer 
		BufferedReader billFileReader;
		try {
			billFileReader = new BufferedReader(new FileReader(billFilePath));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println(BFI_STR1);
			return false;
		}
		
		//Import file lines to temp table 
		try {
				String anEntry;
				while((anEntry = billFileReader.readLine()) != null)				
					new Bill(anEntry,delimiter, this.provider).persistTemp();
				billFileReader.close();
				
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println(BFI_STR2);
			return false;
		}
		
		//Run checks
		
		try {
			totalAmount = Bill.getTotalTempAmount();
			rowCount = Bill.getTempRowCount();
			validBillNoCount = Bill.getTempValidBillNoCount();
			totalValidBillNoAmount = Bill.getTempTotalValidBillNoAmount();
			emptyBillNoCount = Bill.getTempEmptyBillNoCount();
			totalEmptyBillNoAmount = Bill.getTempTotalEmptyBillNoAmount();
			duplicatesCount = Bill.getTempDuplicatesCount();
			existingBillNoCount = Bill.getTempExistingBillNoCount();
			
			
			if(duplicatesCount!=0)
				System.out.println(duplicatesCount + BFI_STR4);				
				
			if(totalEmptyBillNoAmount != 0)
				System.out.println(emptyBillNoCount + BFI_STR5 + "(" + totalEmptyBillNoAmount + ")");
			
			if(existingBillNoCount != 0)
				System.out.println(existingBillNoCount + BFI_STR7);
			
			if(duplicatesCount!=0 || totalEmptyBillNoAmount != 0 || existingBillNoCount != 0)
			{
				System.out.println("Type in 1 to fix'em or anything else to abort");
				int saisie = Integer.parseInt((new BufferedReader(new InputStreamReader(System.in)).readLine()));
				if(saisie != 1)
				{
					System.out.println("Bills import aborted...");
					return false;
				}
				else
				{
					System.out.println("OK - Will try to fix error(s)...");
					return true;
				}
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("Something went wrong!!");
			return false;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("Something went wrong in your reply");
			return false;
		} catch (NumberFormatException n) {
			return false;
			
		}
		
				
		return true;
		
	}
	
	/**
	 * Transfer valid records
	 * from bills temporary table to their final destination
	 * Log the operation afterwards
	 * @return
	 */
	public boolean doImport()
	{
		Bill.emptyTempTable();
		if(!runCheck())
			return false;
		/*Only insert non empty and non existing!!*/
		boolean res = Bill.importBills();
		if(res != true)
		{
			System.out.println(BFI_STR6);
			return false;
		}
		Bill.emptyTempTable();
		System.out.println(BFI_STR8);
		return true;
	}
	
	

}
